const tool = require('../tool');
const fs = require('fs');
const banner_manager = require('../project_config/banner_manager');


//添加/编辑 页面
module.exports = async function bgEditorPageGenerate(module_config, bg_editor_page) {
  var module_config = module_config;

  var bgEditorPageObj = handleInitBgEditorPage(module_config, bg_editor_page);

  var {
    totalTplCode,     //模板
    inputDataContent, //输入data
    pageMethods,      //方法
    mountedCode,
  } = bgEditorPageObj;


  //引入去除未使用的插值字符串
  const finalHandle = require('../module/final_handle'); //最后处理未使用的数据

  //生成文件
  tool.generateFunc({
    tipsText: bg_editor_page.tipsText,
    filePath: `./template/bg_editor_tpl.vue`,
    outputPath: `../src/pages/${module_config.module_name}/${bg_editor_page.bg_editor_page_name}.vue`,
    templateCode: totalTplCode,  //1
    mountedCode: mountedCode,
    methodsCodeFirst: pageMethods,  //2
    // methodsCodeSecond: tableMethodsSecond,
    dataCode: inputDataContent,  //3
    // styleCode: '',
  }, finalHandle);
}


//生成函数 
function handleInitBgEditorPage(module_config, bg_editor_page) {

  var totalTplCode = getTotalTplCode(bg_editor_page);
  var inputDataContent = getInputDataContent(bg_editor_page);
  var mountedCode = getMountedCode(bg_editor_page);
  var pageMethods = getPageMethods(bg_editor_page);

  return {
    totalTplCode,     //模板
    inputDataContent, //输入data
    pageMethods,      //方法
    mountedCode, //加载方法
  }
}


//模板
function getTotalTplCode(bg_editor_page) {
  var { title, minHeight, inputName, editContentChangeMethodName } = bg_editor_page.formData;
  var totalTplCode = `
  <div class="title">${title}</div>
  <div class="bannerSetting">
    <div class="regist_data">
    <bg-editor
      :minHeight="${minHeight}"
      :uploadUrl="formData.uploadApi"
      :content="formData.${inputName}"
      @editorData="${editContentChangeMethodName}"
    ></bg-editor>
    </div>
    <div class="buttonA">
      <el-button type="primary" @click="submit">保存</el-button>
      <el-button type="primary" @click="clear">重置</el-button>
    </div>
  </div>
  `;
  return totalTplCode;
}

//data
function getInputDataContent(bg_editor_page) {
  var { inputName, uploadApi } = bg_editor_page.formData;
  var inputDataContent = `
    formData: {
      value: "",
      fileList: [],
      uploadApi: ${uploadApi}, //图片上传地址
      ${inputName}: "", //编辑内容

      detailList: "", //详情
      showEdit: false,
      detailId: null,
    },
  `;

  return inputDataContent;
}

//mounted
function getMountedCode(bg_editor_page) {
  var mountedCode = `
    this.getDetail();
  `;
  return mountedCode;
}

//method
function getPageMethods(bg_editor_page) {
  var { 
    inputName, 
    editContentChangeMethodName, 
    sysDetailApi,
    settingDataId, } 
  = bg_editor_page.formData;
  
  if(!sysDetailApi) sysDetailApi = `""`;
  // if(!sysDetailApi) sysDetailApi = `""`;
  // if(!sysDetailApi) sysDetailApi = `""`;

  var pageMethods = `
  
    //富文本编辑器
    ${editContentChangeMethodName} (bgEditorStr) {
      var textColor = "#f9e7d8";
      var isGet = /;;; (.*?) ;;;/.test(bgEditorStr);
      var textStr = "";
      if(!isGet) {
        textStr = \`
        <div style=";;; color: \${textColor}; padding: 10px; ;;;"> 
        \${bgEditorStr} </div>\`.replace(/;;; (.*?) ;;;/g, (node, key)=> {
          return \`;;; color: \${textColor}; padding: 10px; ;;;\`;
        })
      } else {
        textStr = bgEditorStr.replace(/;;;(.*?);;;/g, (node, key)=> {
          return \`;;; color: \${textColor}; padding: 10px; ;;;\`;
        })
      }
      console.log(textStr);
      this.formData.content = textStr;
    },

    //保存
    submit() {
      var url = API.sys_setting.config_update;
      const id = this.$route.query.id;
      var param = {
        content: this.Text,
        id,
        type: 1,
      };
      jhTools.post(url, param, null, (res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "保存成功",
          });
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
      });
    },
    
    //获取详情
    getDetail() {
      var url = ${sysDetailApi};
      var param = {
        id: ${settingDataId}
      };
      jhTools.post(url, null, param, (res) => {
        console.log("结果", res);
        if (res.code == 200) {
          this.formData = res.data;
        } else {
          this.$message({ type: "error", message: res.message });
        }
        $("#summernote").summernote("code", this.Text);
      });
    },
  `;
  return pageMethods;
}
